package test11;

import java.io.*;

public class SerializableDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // 初始化对象
        Person person1 = new Person();
        person1.setName("张三");
        person1.setAge(25);
        System.out.println(person1);

        // 序列化对象到文件中
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("aaa"));
        oos.writeObject(person1);
        oos.close();

        // 反序列化
        File file = new File("aaa");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        Person newPerson = (Person) ois.readObject();
        ois.close();
        System.out.println(newPerson.toString());

    }
}
